package com.xqboss.apps.controller.punch;

import com.xqboss.apps.convert.punch.PunchUserRecordConverter;
import com.xqboss.apps.domain.punch.PunchUserRecord;
import com.xqboss.apps.dto.punch.PunchInDto;
import com.xqboss.apps.dto.punch.UserTeamIncomeRecordDto;
import com.xqboss.apps.service.punch.UserPunchService;
import com.xqboss.apps.service.user.UserTeamSettleRecordService;
import com.xqboss.apps.vo.punch.UserPunchRecordVo;
import com.xqboss.apps.vo.punch.UserTeamIncomeRecordVo;
import com.xqboss.common.annotation.RepeatSubmit;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Trent
 * @date 2023/8/24
 */
@Api(tags = {"打卡-打卡"})
@RestController
@RequestMapping("/punch")
public class PunchController extends BaseController {

    @Autowired
    private UserPunchService userPunchService;

    @Autowired
    private PunchUserRecordConverter punchUserRecordConverter;

    @Autowired
    private UserTeamSettleRecordService teamSettleRecordService;

    @RepeatSubmit(interval = 1000, justUrl = true)
    @ApiOperation("用户打卡")
    @PostMapping("in")
    public R<UserPunchRecordVo> punchIn(@Validated @RequestBody PunchInDto params) throws Exception {
        PunchUserRecord record = userPunchService.punchIn(getUserId());
        UserPunchRecordVo vo = punchUserRecordConverter.toVo(record);
        // 判断是否已经达到打卡上限
        // 判断是否设置日打卡上限
        vo.setReachingDayMax(userPunchService.isReachingDayMax(getUserId()));
        return R.ok(vo);
    }

    @ApiOperation("获取队伍打卡增益未读记录")
    @GetMapping("team-income")
    public R<List<UserTeamIncomeRecordVo>> teamIncome() {
        Long userId = getUserId();
        UserTeamIncomeRecordDto dto = new UserTeamIncomeRecordDto();
        dto.setUserId(userId);
        dto.setIsRead(false);
        List<UserTeamIncomeRecordVo> list = teamSettleRecordService.getUserTeamIncomeRecord(dto);
        // 自动将队伍收益记录置为已读
        teamSettleRecordService.setAllRead(userId);
        return R.ok(list);
    }


    @RepeatSubmit(interval = 1000, justUrl = true)
    @ApiOperation("批量用户打卡")
    @PostMapping("/batch")
    public R punchBatch() {
        userPunchService.punchBatch(getUserId());
        return R.ok();
    }
}